/*
 * Copyright (c) 2001-2018 GuaHao.com Corporation Limited. All rights reserved.
 * This software is the confidential and proprietary information of GuaHao Company.
 * ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with GuaHao.com.
 */
package com.bgt.springboot.first.controller;

import com.bgt.springboot.first.dao.UserCopyMapper;
import com.bgt.springboot.first.model.UserCopy;

import java.util.Arrays;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * TODO
 *
 * @author Ricky
 * @version V1.0
 * @since 2018-08-02 18:32
 */
@RestController
@RequestMapping("/uc/")
public class UserCopyController {
    @Autowired
    private UserCopyMapper userCopyMapper;

    /**             var datas = [];
     *                        var params = {
     *                         datas: datas,
     *                         types: types
     *                     };
     *                      type: 'post',
     *                         dataType: 'json',
     *                       contentType:'application/json;charset=UTF-8',//关键是要加上这行
     *                       traditional:true,//这使json格式的字符不会被转码
     *                         url: $GC.guahaoServer + '/medicine/import/ajax/uploadBatchFailData',
     *                         cache: false,
     *                         data: JSON.stringify(params),
     *                         success: function(res) {
     *
     *                             location.reload();
     *                             $GM.smartAlert('删除成功');
     *
     *                         }
     *  json格式
     * {
     * "datas":[{"id":"5","age":"101"},{"id":"6","age":"102"},{"id":"7","age":"103"}],
     * "types":"0"
     * }
     * @param batchdatas
     * @return
     */
    @PostMapping(value = "/updatebatch")
    public Integer updatebatch(@RequestBody BatchData batchdatas){

        System.out.println("-----------------");
        return userCopyMapper.updateList(batchdatas.getDatas());
    }

    /**
     * 参照img/delbatch.png
     * @param ids
     * @return
     */
    @PostMapping(value = "/delbatch")
    public Integer delbatch(@RequestParam(value="ids[]") Integer[] ids){

        System.out.println("---------delbatch--------");

        return userCopyMapper.deleteByIds(Arrays.asList(ids));
    }
    /**
     *
     * @param id,age
     * @return
     */
    @PostMapping(value = "/update/{id}/{age}")
    public Integer updateById(@PathVariable(value = "id") Integer id,@PathVariable(value = "age") Integer age){

        System.out.println("---------delbatch--------");
        UserCopy copy=new UserCopy();
        copy.setId(id);
        copy.setAge(age);

        return userCopyMapper.updateById(copy);
    }
}
